<?php

// Bye bye "Wordpress to WordPress" filter
remove_filter('the_content', 'capital_P_dangit');
remove_filter('the_title', 'capital_P_dangit');
remove_filter('comment_text', 'capital_P_dangit');

// In WordPress 2.9 (or older): Show all pages.
// In WordPress 3.0+: Show a custom menu
function fazio_top_menu()
{
	if(function_exists('wp_nav_menu'))
	{
		wp_nav_menu(array('theme_location' => 'header-menu', 'menu_class' => 'nav', 'fallback_cb' => ''));
	}
}

add_filter('gallery_style', 'fazio_replace_gallery_style');

function fazio_replace_gallery_style($style)
{
	global $selector, $id;
	return "<div id='{$selector}' class='gallery galleryid-{$id}'>";
}


// If slideshow is active, add jCycle JS code to the bottom
if(fazio_option('slider_status') == 'on')
{
	add_action('wp_footer', 'fazio_add_coin_slider');
}
function fazio_add_coin_slider()
{
	if(is_home()) :
?>
		<script type="text/javascript"> 
		jQuery(document).ready(function($){
			$('#slideshowwrap').css('display', 'block');
				
			$('#slideshow').coinslider({
				effect: '<?php echo fazio_option('slider_effect'); ?>',
				delay: <?php echo fazio_option('slider_delay'); ?>,
				width: <?php echo fazio_option('slider_width'); ?>,
				height: <?php echo fazio_option('slider_height'); ?>,
				opacity: <?php echo fazio_option('slider_opacity'); ?>, // opacity of title and navigation
				titleSpeed: <?php echo fazio_option('slider_title_speed'); ?>, // speed of title appereance in ms
				hoverPause: <?php if(fazio_option('slider_pause_on_hover') == 'on') { ?>true<?php } else { ?>false<?php } ?> // pause on hover
			});
		});
		</script> 
<?php
	endif;
}

// Add "Delete" link next to "Edit" comment link.
function get_delete_comment_link( $comment_id = 0 ) {
	$comment = &get_comment($comment_id);
	$post = &get_post($comment->comment_post_ID);

	if($post->post_type == 'page')
	{
		if(!current_user_can('edit_page', $post->ID))
		{
			return;
		}
	}
	else
	{
		if(!current_user_can('edit_post', $post->ID))
		{
			return;
		}
	}

	$location = admin_url('comment.php?action=cdc&amp;c=') . $comment->comment_ID;
	return $location;
}

function delete_comment_link($link, $before, $after) {
	global $comment, $post;

	if($post->post_type == 'page')
	{
		if(!current_user_can('edit_page', $post->ID))
		{
			return;
		}
	}
	else
	{
		if(!current_user_can('edit_post', $post->ID))
		{
			return;
		}
	}

	if($link === null)
	{
		$link = __('Delete', 'fazio');
	}

	$link = '<a class="comment-delete-link" href="' . get_delete_comment_link($comment->comment_ID) . '" title="' . __('Delete comment', 'fazio') . '">' . $link . '</a>';
	echo $before, $link, $after;
}

// Add Twitter field to user's profile
add_filter('user_contactmethods', 'add_twitter', 10, 1); 
function add_twitter($fields) 
{ 
	$fields['user_twitter'] = __('Twitter', 'fazio'); 
 	return $fields; 
}

// Disable rare characteres for titles
remove_filter('the_title', 'wptexturize');

// Used to localize some strings.
if(!function_exists('get_the_author_posts_link'))
{
	function get_the_author_posts_link() {
		global $authordata;
	
		$link = sprintf(
			'<a href="%1$s" title="%2$s">%3$s</a>',
			get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
			esc_attr( sprintf( __( 'Posts by %s', 'fazio'), get_the_author() ) ),
			get_the_author()
		);
		
		return $link;
	}
}

// Get data from logged user - From http://www.devdevote.com/cms/wordpress-hacks/get_user/
function get_user($field = 'ID')
{
	global $current_user;
	get_currentuserinfo();

	echo $current_user->$field;
}

// comments_number count comments & pings. This function counts only comments.
add_filter('get_comments_number', 'comment_count', 0);
function comment_count($count) {
	global $id;
	
	$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
	return count($comments_by_type['comment']);
}

// Comments template
function fazio_comments($comment, $args, $depth)
{
	$GLOBALS['comment'] = $comment;
?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
		<div class="comment" id="comment-<?php comment_ID(); ?>">
			<div class="commentbox">
				<div class="comment_text"><?php comment_text(); ?></div>
				<div class="avatar"><?php echo get_avatar($comment, $size = '50'); ?></div>
				<div class="clear"></div>
			</div>
			
			<span class="author"><?php _e('By', 'fazio'); ?> <?php comment_author_link(); ?></span>
			<span class="date"><?php comment_date(); ?> @ <?php comment_time(); ?></span>
			<?php edit_comment_link(__('Edit', 'fazio'), '<span class="edit"> - ', '</span>'); ?>
			<?php delete_comment_link(__('Delete', 'fazio'), '<span class="delete"> - ', '</span>'); ?>
			<div class="reply">
				<?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))); ?>
			</div>
		</div>
<?php
}

// Pings template
function fazio_pings($comment, $args, $depth)
{
	$GLOBALS['comment'] = $comment;
?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
		<p><?php comment_author_link(); ?> 
		<?php edit_comment_link(__('Edit', 'fazio'), '<span class="edit"> - ', '</span>'); ?> 
		<?php delete_comment_link(__('Delete', 'fazio'), '<span class="delete"> - ', '</span>'); ?></p>
<?php
}